/*
 * @(#)DbConfigService.java   1.0  2021年11月24日
 * 
 * Copyright (c)	2014-2020. All Rights Reserved.	GuangZhou hhmk Technology Company LTD.
 */
package com.swift.dts.service;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.swift.core.auth.Login;
import com.swift.core.model.ServiceRequest;
import com.swift.core.model.data.DataModel;
import com.swift.core.model.data.MapDataModel;
import com.swift.core.model.parser.DataJsonParser;
import com.swift.core.service.SynInterface;
import com.swift.dts.dao.DbConfigDao;
import com.swift.dts.loadconfig.model.DtsDataBase;
import com.swift.dts.loadconfig.model.ViewDtsDataBase;
import com.swift.dts.source.canal.LocalFileCanalConfig;
import com.swift.util.bean.BeanUtil;
import com.swift.util.exec.PathUtil;
import com.swift.util.type.ByteUtil;
import com.swift.util.type.TypeUtil;

/**
 * 添加说明 
 * @author zhengjiajin
 * @version 1.0 2021年11月24日
 */
@Service("dbConfigList")
@Login
public class DbConfigList implements SynInterface {

    @Autowired
    private DbConfigDao dbConfigDao;
    /** 
     * @see com.swift.core.service.SynInterface#doService(com.swift.core.model.ServiceRequest)
     */
    @Override
    public DataModel doService(ServiceRequest req) {
        List<DtsDataBase> list = dbConfigDao.list();
        if(TypeUtil.isNull(list)) return null;
        DataModel res = new MapDataModel();
        List<ViewDtsDataBase> resList = new LinkedList<ViewDtsDataBase>();
        for(DtsDataBase db:list) {
            ViewDtsDataBase view = new ViewDtsDataBase();
            BeanUtil.copyProperties(view, db);
            setSynRetard(view);
            resList.add(view);
        }
        res.addObject("list", resList);
        return res;
    }
    
    private long day = 24*60*60;
    
    private long hour = 60*60;
    
    private long minute = 60;
    
    private void setSynRetard(ViewDtsDataBase view) {
        setNoStart(view);
        if(!view.getIsSource()) return;
        DataModel meta = metaData(view.getKey());
        if(meta==null) return;
        long timestamp = meta.getLong("clientDatas.cursor.postion.timestamp",0);   
        if(timestamp==0) return;
        long retard = (System.currentTimeMillis()-timestamp)/1000;
        view.setSynRetard(retard);
        StringBuffer sb = new StringBuffer();
        if(retard>day) {
            long time = retard/day;
            retard=retard%day;
            sb.append(time+"天");
        }
        if(retard>hour) {
            long time = retard/hour;
            retard=retard%hour;
            sb.append(time+"小时");
        }
        if(retard>minute) {
            long time = retard/minute;
            retard=retard%minute;
            sb.append(time+"分钟");
        }
        if(retard>0) {
            sb.append(retard+"秒");
        }
        view.setSynRetardRem(sb.toString());
    }
    
    private void setNoStart(ViewDtsDataBase view) {
        view.setSynRetard(0);
        if(view.getIsSource()) {
            view.setSynRetardRem("未启动");
        }else {
            view.setSynRetardRem("---");
        }
    }
    
    private DataModel metaData(String key) {
        byte[] bdata =  loadFile("/"+LocalFileCanalConfig.CANAL_PATH+"/"+key+"/meta.dat");
        if(TypeUtil.isNull(bdata)) return null;
        DataModel data = DataJsonParser.jsonToObject(new String(bdata));
        return data;
    }
    
    private byte[] loadFile(String metaDat) {
        try {
            InputStream in = new FileInputStream(PathUtil.findOnClassPath(metaDat));
            byte[] jsonByte = ByteUtil.inputToByte(in);
            try {
                in.close();
            } catch (IOException e) {
            }
            return jsonByte;
        } catch (Exception e) {
            return null;
        }
    }

}
